#ifndef _UTIL_H
#define _UTIL_H
#include <iostream>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <experimental/filesystem>
#include <sstream>
namespace tqbcloud
{
    namespace fs = std::experimental::filesystem;
    class Fileutil
    {
    private:
        std::string _name;

    public:
        Fileutil(const std::string &name) : _name(name){};
        int64_t FileSize()
        {
            struct stat st;
            if (stat(_name.c_str(), &st) == -1)
            {
                std::cout << "stat erro" << std::endl;
            }
            else
            {
                return st.st_size;
            }
        }
        time_t Lastacctime()
        {
            struct stat st;
            if (stat(_name.c_str(), &st) == -1)
            {
                std::cout << "stat erro" << std::endl;
            }
            else
            {
                return st.st_atime;
            }
        }
        time_t Lastmotime()
        {
            struct stat st;
            if (stat(_name.c_str(), &st) == -1)
            {
                std::cout << "stat erro" << std::endl;
            }
            else
            {
                return st.st_mtime;
            }
        }
        std::string FileName()
        {
            int pos = _name.find_last_of("/\\");
            if (pos == std::string::npos)
            {
                return _name;
            }
            return _name.substr(pos + 1);
        };
        bool GetPosLen(std::string *content, size_t pos, size_t len)
        {
            std::ifstream f;
            f.open(_name.c_str(), std::ios::binary);
            if (f.is_open() == false)
            {
                std::cout << "open erro" << std::endl;
                return false;
            }
            f.seekg(pos, f.beg);
            content->resize(len);
            f.read(&((*content)[0]), len);
            if (f.good() == false)
            {
                std::cout << "read erro" << std::endl;
                return false;
            }
            return true;
            f.close();
        }
        bool GetContent(std::string *content)
        {
            if (GetPosLen(content, 0, FileSize()))
            {
                return true;
            }
            return false;
        }
        bool SetContent(const std::string *content)
        {
            std::ofstream f;
            f.open(_name.c_str(), std::ios::binary);
            if (f.is_open() == false)
            {
                std::cout << "open erro" << std::endl;
                return false;
            }
            f.write(&((*content)[0]), content->size());
            if (f.good() == false)
            {
                std::cout << "write erro" << std::endl;
                return false;
            }
            return true;
            f.close();
        }
        bool Exists()
        {
            if (fs::exists(_name))
            {
                return true;
            }
            return false;
        }
        bool CreateDirectory_()
        {
            if (fs::create_directories(_name))
            {
                return true;
            }
            return false;
        }
        bool ScanDirectory(std::vector<std::string> *arry)
        {
            for (const fs::directory_entry &entry : fs::directory_iterator{_name.c_str()})
            {
                if (fs::is_directory(entry))
                {
                    continue;
                }
                arry->push_back(fs::path(entry).relative_path().string());
            }
            return true;
        }
        bool Remove(const std::string& filname){
             int ret=remove(filname.c_str());
             if(ret==-1){
                std::cout<<"remove erro"<<std::endl;
             }
        }
    };

};
#endif